﻿
using System.Collections.Generic;
using System.Linq;
using Gola.VN.Scb.Web.Bll;
using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Presentation.Models
{
    class ProductService : BaseService
    {
        internal List<CategoryEntity> DisplayingCategories { get; private set; }

        internal ProductService(MainBiz mainBiz)
            : base(mainBiz)
        {
        }

        internal IEnumerable<ProductEntity> GetProducts()
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetProducts;

            _mainBiz.Execute(package);

            return package.Output as IEnumerable<ProductEntity>;
        }

        internal ProductEntity GetProductById(int id)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetProductById;
            package.Input = id;
            _mainBiz.Execute(package);

            return package.Output as ProductEntity;
        }

        internal List<ProductEntity> GetProductsByTitle(string title)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetProductsByTitle;
            package.Input = title;
            _mainBiz.Execute(package);

            return package.Output as List<ProductEntity>;
        }

        internal List<ProductEntity> GetProductsByCategoryId(int categoryId, bool orderByViewCount = true)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetProductsByCategoryId;
            package.MultiInput = new List<object>();
            package.MultiInput.Add(categoryId);
            package.MultiInput.Add(orderByViewCount);

            _mainBiz.Execute(package);

            return package.Output as List<ProductEntity>;
        }

        internal List<ProductEntity> GetProductsByProviderIdAndCategoryName(string categoryName, int providerId)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetProductsByProviderIdAndCategoryName;
            package.MultiInput = new List<object>();
            package.MultiInput.Add(categoryName);
            package.MultiInput.Add(providerId);
            _mainBiz.Execute(package);

            return package.Output as List<ProductEntity>;
        }

        internal List<ProductEntity> GetProductsByProductTitle(int productId, string productTitle)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetProductsByProductTitle;
            package.MultiInput = new List<object>();
            package.MultiInput.Add(productId);
            package.MultiInput.Add(productTitle);
            _mainBiz.Execute(package);

            return package.Output as List<ProductEntity>;
        }

        internal List<ProductEntity> GetOthersProductByTitle(string title, List<int> exclusiveroductIds, int pageIndex)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetOthersProductByTitle;
            package.MultiInput = new List<object>();
            package.MultiInput.Add(title);
            package.MultiInput.Add(exclusiveroductIds);
            package.MultiInput.Add(pageIndex);
            _mainBiz.Execute(package);

            return package.Output as List<ProductEntity>;
        }

        internal List<ProductEntity> GetOthersProductByCategoryId(int categoryId, List<int> exclusiveroductIds, int pageIndex)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetOthersProductByCategoryId;
            package.MultiInput = new List<object>();
            package.MultiInput.Add(categoryId);
            package.MultiInput.Add(exclusiveroductIds);
            package.MultiInput.Add(pageIndex);
            _mainBiz.Execute(package);

            return package.Output as List<ProductEntity>;
        }

        internal void UpdateProductViewCount(int productId)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.UpdateProductViewCount;
            package.Input = productId;

            _mainBiz.Execute(package);
        }

        internal bool InsertProduct(ProductEntity product)
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.InsertProduct;
            package.Input = product;

            _mainBiz.Execute(package);

            int result = System.Convert.ToInt32(package.Output);

            return result > 0 ? true : false;
        }

        internal CategoryEntity GetRandomCategory()
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetRandomCategory;

            _mainBiz.Execute(package);

            var category = package.Output as CategoryEntity;
            if (DisplayingCategories != null && !DisplayingCategories.Contains(category))
            {
                DisplayingCategories.Add(category);
            }
            return category;
        }

        private List<CategoryEntity> GetHostCategories()
        {
            BizPackage package = new BizPackage();
            package.Model = ModelType.Product;
            package.Event = BizEvent.GetHostCategories;

            _mainBiz.Execute(package);

            DisplayingCategories = package.Output as List<CategoryEntity>;
            return package.Output as List<CategoryEntity>;
        }

        //get host products 
        internal List<ProductEntity> GetHostProducts()
        {
            List<ProductEntity> result = new List<ProductEntity>();
            var categories = GetHostCategories();
            foreach (var categoryEntity in categories)
            {
                result.AddRange(GetProductsByCategoryId(categoryEntity.Id).Take(4));
            }

            return result;
        }

        internal List<ProductEntity> GetRandomProducts(int numberItems = 5)
        {
            var products = GetProductsByCategoryId(GetRandomCategory().Id);
            while (products == null || products.Count <= 0)
            {
                products = GetProductsByCategoryId(GetRandomCategory().Id);
            }

            return products.Take(numberItems).ToList();
        }
    }
}